package com.sskj.qiangjin.ui.fragment;


import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.presenter.AddPhotoFragmentPresenter;

import java.io.File;
import java.util.List;

import butterknife.BindView;

import static android.app.Activity.RESULT_OK;

@Route(path = RConfig.APP_FRAGMENT_ADD_PHOTO)
public class AddPhotoFragment extends BaseFragment<AddPhotoFragmentPresenter> {
    @BindView(R.id.iv_add_photo)
    ImageView ivAddPhoto;
    @BindView(R.id.btn_add_photo)
    Button btnAddPhoto;
    private String mImagePath;
    private String imagePath;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_add_photo;
    }

    @Override
    public AddPhotoFragmentPresenter getPresenter() {
        return new AddPhotoFragmentPresenter();
    }

    @Override
    public void initView() {
        ClickUtil.click(ivAddPhoto, () -> {
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .selectionMode(PictureConfig.SINGLE)
                    .hideBottomControls(true)
                    .enableCrop(true)
                    .withAspectRatio(1, 1)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadImg(new File(mImagePath));
        }
    }

    public void uploadSuccess(String imgUrl) {
        this.imagePath = imgUrl;
        ImageUtil.setImage(mImagePath, ivAddPhoto);
        ClickUtil.click(btnAddPhoto, () -> {
            if (TextUtils.isEmpty(imgUrl)) {
                ToastUtil.showShort("头像未上传成功");
                return;
            }
            mPresenter.uploadPhoto(imgUrl);
        });
    }

    public void success() {
        PictureFileUtils.deleteCacheDirFile(getActivity());
        getActivity().finish();
    }
}
